/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Util.ConSQL;
import entity.TaiKhoanEntity;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AnhPhuong
 */
public class TaiKhoanDao {

    public ArrayList<TaiKhoanEntity> selectAll() {
        TaiKhoanEntity account;
        ArrayList<TaiKhoanEntity> list = new ArrayList<TaiKhoanEntity>();
        try {
            String sql = "Select * from account";
            CallableStatement cs = ConSQL.getCon().prepareCall(sql);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                account = new TaiKhoanEntity();
                account.setId(rs.getInt("id"));
                account.setUserName(rs.getString("userName"));
                account.setUserName(rs.getString("pass"));
                account.setUserName(rs.getString("quyenHan"));
                list.add(account);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TaiKhoanDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public boolean insert(TaiKhoanEntity account) {
        try {
            String sql = "Inset into account value '?','?','?'";
            CallableStatement cs = ConSQL.getCon().prepareCall(sql);
            cs.setString(1, account.getUserName());
            cs.setString(1, account.getPassWord());
            cs.setString(1, account.getQuyenHan());
            return (cs.executeUpdate() == 1);
        } catch (SQLException ex) {
            Logger.getLogger(TaiKhoanDao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean update(TaiKhoanEntity account) {
        try {
            PreparedStatement ps = ConSQL.getCon().prepareStatement("update account set  userName=?,pass=?,quyenHan=?");

            ps.setString(1, account.getUserName());
            ps.setString(1, account.getPassWord());
            ps.setString(1, account.getQuyenHan());
            return (ps.executeUpdate() == 1);
        } catch (SQLException ex) {
            Logger.getLogger(TaiKhoanDao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean updatePass(TaiKhoanEntity account) {
        try {
            String sql = "update account set pass=? where userName=?";
            CallableStatement cs = ConSQL.getCon().prepareCall(sql);
            cs.setString(1, account.getUserName());
            cs.setString(2, account.getPassWord());
            return (cs.executeUpdate() == 1);
        } catch (SQLException ex) {
            Logger.getLogger(TaiKhoanDao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean delete(TaiKhoanEntity account) {
        try {
            String sql = "delete * from account where id=?";
            CallableStatement cs = ConSQL.getCon().prepareCall(sql);
            cs.setInt(1, account.getId());
            return (cs.executeUpdate() == 1);
        } catch (SQLException ex) {
            Logger.getLogger(TaiKhoanDao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
